<?php
/**
 * AdminUser
 * 
 * @author misha
 * @version 
 */
require_once 'Zend/Db/Table/Abstract.php';
class MSA_Model_AdminUser extends Zend_Db_Table_Abstract
{
    /**
     * The default table name 
     */
    protected $_name = 'admin_user';
    
    public function updateLoginTime($id)
    {
    	$row = $this->find($id)->current();
    	if ($row) {
    		$row->last_login = date("Y-m-d H:i:s");
    		$row->save();
    	}
    }
    
    public function updateToken($id, $token) 
    {
    	$row = $this->find($id)->current();
    	if ($row) {
    		$row->token = $token;
    		$row->save();
    	}
    }
    
    public function changePassword($id, $oldPassowrd, $newPassword)
    {
    	if ($oldPassowrd === $newPassword) {
    		throw new MSA_Model_Exception('旧密码与新密码一致', 1);
    	}
    	
    	$query = $this->select()
    					->where('id = ?', $id, Zend_Db::INT_TYPE)
    					->where('password = ?', md5($oldPassowrd));
    					
    	$row = $this->fetchRow($query);
    	
    	if ($row) {
    		$result = $this->find($id)->current();
    		$result->password = md5($newPassword);
    		$updateId = $result->save();
    		if ($updateId) {
    			return true;
    		}
    		else {
    			throw new MSA_Model_Exception('未知错误', 500);
    		}
    	}
    	else {
    		throw new MSA_Model_Exception('旧密码不正确', 2);
    	}
    }
}
